namespace CIS.Customer
{
    using CISLib;
    using System;
    using System.IO;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    public class OtherCustomerPicPage : Page
    {
        protected Button AddBtn;
        protected DropDownList DayDdl;
        protected CheckBox EditChk;
        protected Image iBtnCustomer;
        protected ImageButton iBtnGray8;
        protected ImageButton iBtnOrange0;
        protected ImageButton iBtnOrange1;
        protected ImageButton iBtnOrange2;
        protected ImageButton iBtnOrange3;
        protected ImageButton iBtnOrange4;
        protected ImageButton iBtnOrange5;
        protected ImageButton iBtnOrange6;
        protected ImageButton iBtnOrange7;
        protected TextBox ImagePersonTxb;
        protected TextBox ImageReason;
        protected TextBox ImageVenueTxb;
        protected Label lbDate;
        protected Label lbEdit;
        protected Label lbImagePerson;
        protected Label lbImageVenue;
        protected Label lbMeetTime;
        protected Label lbMonth;
        protected Label lbSaveMsg;
        protected Label lbSummary;
        protected Label lbYear;
        protected DropDownList MonthDdl;
        protected UserPrincipal Principal = null;
        protected ImageButton returnImgBtn;
        protected Repeater rptImage;
        protected HtmlInputFile UpdateImageFile;
        protected DropDownList YearDdl;

        private void AddBtn_Click(object sender, EventArgs e)
        {
            int ImageSize = this.UpdateImageFile.PostedFile.ContentLength;
            string ImageType = this.UpdateImageFile.PostedFile.ContentType;
            Stream ImageStream = this.UpdateImageFile.PostedFile.InputStream;
            byte[] ImageContent = new byte[ImageSize];
            int Status = ImageStream.Read(ImageContent, 0, ImageSize);
            string strImageVenue = this.ImageVenueTxb.Text.Trim();
            string strImagePerson = this.ImagePersonTxb.Text.Trim();
            string strImageReason = this.ImageReason.Text.Trim();
            try
            {
                DateTime MeetingDate = new DateTime(0x708, 1, 1);
                if (((this.YearDdl.SelectedItem.Text != "----") || (this.MonthDdl.SelectedItem.Text != "--")) || (this.DayDdl.SelectedItem.Text != "--"))
                {
                    MeetingDate = new DateTime(Convert.ToInt32(this.YearDdl.SelectedItem.Text), Convert.ToInt32(this.MonthDdl.SelectedItem.Text), Convert.ToInt32(this.DayDdl.SelectedItem.Text));
                }
                if (this.EditChk.Checked)
                {
                    ManageCustomer.UpdateCustomerImageInfo(new Guid(base.Request.QueryString["CustomerId"]), new Guid(this.Page.Session["ImageId"].ToString()), MeetingDate, strImageVenue, strImagePerson, strImageReason, ImageType, ImageContent);
                    ManageLog.InsertLog(this.Principal.SysUser.UserId, this.Principal.SysUser.LoginName, DateTime.Now, "UpdatePic", "IP:" + base.Request.UserHostAddress + " DNS:" + base.Request.UserHostName, ManageCustomer.GetCustomerBasicInfoViaId(new Guid(base.Request.QueryString["CustomerId"])).Name, new Guid(base.Request.QueryString["CustomerId"]));
                }
                else
                {
                    ManageCustomer.InsertNewCustomerImageInfo(new Guid(base.Request.QueryString["CustomerId"]), MeetingDate, strImageVenue, strImagePerson, strImageReason, ImageType, ImageContent);
                    ManageLog.InsertLog(this.Principal.SysUser.UserId, this.Principal.SysUser.LoginName, DateTime.Now, "InsertPic", "IP:" + base.Request.UserHostAddress + " DNS:" + base.Request.UserHostName, ManageCustomer.GetCustomerBasicInfoViaId(new Guid(base.Request.QueryString["CustomerId"])).Name);
                }
                this.EditChk.Checked = false;
                this.InitPage();
                this.ImageVenueTxb.Text = string.Empty;
                this.ImagePersonTxb.Text = string.Empty;
                this.ImageReason.Text = string.Empty;
            }
            catch (Exception ex)
            {
                this.lbSaveMsg.Text = ex.Message;
                return;
            }
            this.BindGrid();
        }

        private void AddImage()
        {
        }

        protected string AddStrPrefix(object obj, string strPre)
        {
            if (obj.ToString() == string.Empty)
            {
                return "";
            }
            string language = this.Session["language"].ToString();
            string path = base.Server.MapPath("~/LanguageConfig.xml");
            return (ManageSystemConfig.GetSystemSetLanguage("OtherCustomerPicPage", strPre, language, path) + obj.ToString());
        }

        private void BindGrid()
        {
            this.rptImage.DataSource = ManageCustomer.GetCustomerImageInfo(new Guid(base.Request.QueryString["CustomerId"]));
            this.rptImage.DataBind();
        }

        protected string DecryptString(object obj)
        {
            return ManageOther.DecodeWithBytes(obj.ToString());
        }

        private void iBtnOrange0_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                string customerId = base.Request.QueryString["CustomerId"].ToString();
                base.Response.Redirect("~/Customer/CustomerBasicInfoPage.aspx?IsModify=True&CustomerId=" + customerId.ToString(), true);
            }
            catch (Exception)
            {
            }
        }

        private void iBtnOrange1_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                string customerId = base.Request.QueryString["CustomerId"].ToString();
                base.Response.Redirect("~/Customer/CustomerInfoPersonalPage.aspx?IsModify=True&CustomerId=" + customerId.ToString(), true);
            }
            catch (Exception)
            {
            }
        }

        private void iBtnOrange2_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                string customerId = base.Request.QueryString["CustomerId"].ToString();
                base.Response.Redirect("~/Customer/CustomerInfoPreferencePage.aspx?IsModify=True&CustomerId=" + customerId.ToString(), true);
            }
            catch (Exception)
            {
            }
        }

        private void iBtnOrange3_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                string customerId = base.Request.QueryString["CustomerId"].ToString();
                base.Response.Redirect("~/Customer/CustomerInfoHealthCostumePage.aspx?IsModify=True&CustomerId=" + customerId.ToString(), true);
            }
            catch (Exception)
            {
            }
        }

        private void iBtnOrange4_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                string customerId = base.Request.QueryString["CustomerId"].ToString();
                base.Response.Redirect("~/Customer/CustomerInfoTravelPage.aspx?IsModify=True&CustomerId=" + customerId.ToString(), true);
            }
            catch (Exception)
            {
            }
        }

        private void iBtnOrange5_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                string customerId = base.Request.QueryString["CustomerId"].ToString();
                base.Response.Redirect("~/Customer/CustomerInfoStudyPage.aspx?IsModify=True&CustomerId=" + customerId.ToString(), true);
            }
            catch (Exception)
            {
            }
        }

        private void iBtnOrange6_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                string customerId = base.Request.QueryString["CustomerId"].ToString();
                base.Response.Redirect("~/Customer/CustomerInfoWorkPage.aspx?IsModify=True&CustomerId=" + customerId.ToString(), true);
            }
            catch (Exception)
            {
            }
        }

        private void iBtnOrange7_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                string customerId = base.Request.QueryString["CustomerId"].ToString();
                base.Response.Redirect("~/Customer/CustomerInfoFamilyPage.aspx?IsModify=True&CustomerId=" + customerId.ToString(), true);
            }
            catch (Exception)
            {
            }
        }

        private void InitializeComponent()
        {
            this.iBtnOrange0.Click += new ImageClickEventHandler(this.iBtnOrange0_Click);
            this.iBtnOrange1.Click += new ImageClickEventHandler(this.iBtnOrange1_Click);
            this.iBtnOrange2.Click += new ImageClickEventHandler(this.iBtnOrange2_Click);
            this.iBtnOrange3.Click += new ImageClickEventHandler(this.iBtnOrange3_Click);
            this.iBtnOrange4.Click += new ImageClickEventHandler(this.iBtnOrange4_Click);
            this.iBtnOrange5.Click += new ImageClickEventHandler(this.iBtnOrange5_Click);
            this.iBtnOrange6.Click += new ImageClickEventHandler(this.iBtnOrange6_Click);
            this.iBtnOrange7.Click += new ImageClickEventHandler(this.iBtnOrange7_Click);
            this.rptImage.ItemCommand += new RepeaterCommandEventHandler(this.rptImage_ItemCommand);
            this.AddBtn.Click += new EventHandler(this.AddBtn_Click);
            this.returnImgBtn.Click += new ImageClickEventHandler(this.returnImgBtn_Click);
            base.Load += new EventHandler(this.Page_Load);
        }

        private void InitPage()
        {
            this.SetFromImage();
            this.SetFrom();
            this.YearDdl.Items.Clear();
            this.MonthDdl.Items.Clear();
            this.DayDdl.Items.Clear();
            ListItem tmpItem = new ListItem();
            tmpItem.Text = "----";
            this.YearDdl.Items.Add(tmpItem);
            ListItem tmpItem1 = new ListItem();
            tmpItem1.Text = "--";
            this.MonthDdl.Items.Add(tmpItem1);
            this.DayDdl.Items.Add(tmpItem1);
            for (int i = DateTime.Now.Year; i >= 0x78a; i--)
            {
                ListItem YearItem = new ListItem();
                YearItem.Text = i.ToString();
                this.YearDdl.Items.Add(YearItem);
            }
            for (int i = 1; i <= 12; i++)
            {
                ListItem MonthItem = new ListItem();
                MonthItem.Text = i.ToString();
                this.MonthDdl.Items.Add(MonthItem);
            }
            for (int i = 1; i <= 0x1f; i++)
            {
                ListItem DateItem = new ListItem();
                DateItem.Text = i.ToString();
                this.DayDdl.Items.Add(DateItem);
            }
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
            this.Principal = this.Context.User as UserPrincipal;
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                this.SelectAuth();
            }
        }

        private void returnImgBtn_Click(object sender, ImageClickEventArgs e)
        {
            base.Response.Redirect("~/Customer/CustomerInfoOtherPage.aspx?IsModify=True&CustomerId=" + base.Request.QueryString["CustomerId"].ToString(), true);
        }

        private void rptImage_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (this.Principal.OwnRole.TypeB == 0)
            {
                base.Response.Write("<script> alert('没有权限操作！') </script>");
            }
            else
            {
                Guid customerId = new Guid(base.Request.QueryString["CustomerId"]);
                if ((this.Principal.SysUser.IsAdmin <= 0) && !ManageSystemConfig.IsThisCustomerMaster(this.Principal.SysUser.UserId, customerId))
                {
                    base.Response.Write("<script> alert('没有权限操作！') </script>");
                }
                else
                {
                    string cmd = e.CommandName;
                    string strTmp = (string) e.CommandArgument;
                    Guid imageId = new Guid(strTmp);
                    switch (cmd)
                    {
                        case "delete":
                            ManageCustomer.RemoveCustomerImageInfoViaId(imageId);
                            this.BindGrid();
                            break;

                        case "modify":
                        {
                            CustomerImageInfo customerImageInfo = ManageCustomer.GetCustomerImageInfoViaId(imageId);
                            if (customerImageInfo != null)
                            {
                                try
                                {
                                    this.ImageVenueTxb.Text = customerImageInfo.ImageVenue;
                                    this.ImagePersonTxb.Text = customerImageInfo.ImagePerson;
                                    this.ImageReason.Text = customerImageInfo.ImageReason;
                                    if (((customerImageInfo.ImageDate.Year == 0x708) && (customerImageInfo.ImageDate.Month == 1)) && (customerImageInfo.ImageDate.Day == 1))
                                    {
                                        this.YearDdl.SelectedIndex = 0;
                                        this.MonthDdl.SelectedIndex = 0;
                                        this.DayDdl.SelectedIndex = 0;
                                    }
                                    else
                                    {
                                        for (int i = 0; i < this.YearDdl.Items.Count; i++)
                                        {
                                            if (this.YearDdl.Items[i].Text == customerImageInfo.ImageDate.Year.ToString())
                                            {
                                                this.YearDdl.SelectedIndex = i;
                                                break;
                                            }
                                        }
                                        for (int i = 0; i < this.MonthDdl.Items.Count; i++)
                                        {
                                            if (this.MonthDdl.Items[i].Text == customerImageInfo.ImageDate.Month.ToString())
                                            {
                                                this.MonthDdl.SelectedIndex = i;
                                                break;
                                            }
                                        }
                                        for (int i = 0; i < this.DayDdl.Items.Count; i++)
                                        {
                                            if (this.DayDdl.Items[i].Text == customerImageInfo.ImageDate.Day.ToString())
                                            {
                                                this.DayDdl.SelectedIndex = i;
                                                break;
                                            }
                                        }
                                    }
                                    this.EditChk.Checked = true;
                                    this.Page.Session.Add("ImageId", customerImageInfo.ImageId.ToString());
                                }
                                catch (Exception)
                                {
                                }
                            }
                            break;
                        }
                    }
                }
            }
        }

        private void SelectAuth()
        {
            if (this.Principal.OwnRole.TypeB == 0)
            {
                this.InitPage();
                this.BindGrid();
                this.ImageVenueTxb.Visible = false;
                this.ImagePersonTxb.Visible = false;
                this.ImageReason.Visible = false;
                this.lbMeetTime.Visible = false;
                this.lbYear.Visible = false;
                this.lbMonth.Visible = false;
                this.lbDate.Visible = false;
                this.lbImageVenue.Visible = false;
                this.lbImagePerson.Visible = false;
                this.lbSaveMsg.Visible = false;
                this.lbEdit.Visible = false;
                this.YearDdl.Visible = false;
                this.MonthDdl.Visible = false;
                this.DayDdl.Visible = false;
                this.EditChk.Visible = false;
                this.UpdateImageFile.Visible = false;
                this.lbSummary.Visible = false;
                this.AddBtn.Visible = false;
            }
            else if (this.Principal.OwnRole.TypeB == 1)
            {
                this.InitPage();
                this.BindGrid();
            }
            Guid customerId = new Guid(base.Request.QueryString["CustomerId"]);
            if ((this.Principal.SysUser.IsAdmin <= 0) && !ManageSystemConfig.IsThisCustomerMaster(this.Principal.SysUser.UserId, customerId))
            {
                this.ImageVenueTxb.Visible = false;
                this.ImagePersonTxb.Visible = false;
                this.ImageReason.Visible = false;
                this.lbMeetTime.Visible = false;
                this.lbYear.Visible = false;
                this.lbMonth.Visible = false;
                this.lbDate.Visible = false;
                this.lbImageVenue.Visible = false;
                this.lbImagePerson.Visible = false;
                this.lbSaveMsg.Visible = false;
                this.lbEdit.Visible = false;
                this.YearDdl.Visible = false;
                this.MonthDdl.Visible = false;
                this.DayDdl.Visible = false;
                this.EditChk.Visible = false;
                this.lbSummary.Visible = false;
                this.AddBtn.Visible = false;
                this.UpdateImageFile.Visible = false;
            }
        }

        private void SetFrom()
        {
            string language = this.Session["language"].ToString();
            string path = base.Server.MapPath("~/LanguageConfig.xml");
            this.lbMeetTime.Text = ManageSystemConfig.GetSystemSetLanguage("OtherCustomerPicPage", "lbMeetTime", language, path);
            this.lbYear.Text = ManageSystemConfig.GetSystemSetLanguage("OtherCustomerPicPage", "lbYear", language, path);
            this.lbMonth.Text = ManageSystemConfig.GetSystemSetLanguage("OtherCustomerPicPage", "lbMonth", language, path);
            this.lbDate.Text = ManageSystemConfig.GetSystemSetLanguage("OtherCustomerPicPage", "lbDate", language, path);
            this.lbImageVenue.Text = ManageSystemConfig.GetSystemSetLanguage("OtherCustomerPicPage", "lbImageVenue", language, path);
            this.lbImagePerson.Text = ManageSystemConfig.GetSystemSetLanguage("OtherCustomerPicPage", "lbImagePerson", language, path);
            this.lbSaveMsg.Text = ManageSystemConfig.GetSystemSetLanguage("OtherCustomerPicPage", "lbSaveMsg", language, path);
            this.lbEdit.Text = ManageSystemConfig.GetSystemSetLanguage("OtherCustomerPicPage", "lbEdit", language, path);
            this.lbSummary.Text = ManageSystemConfig.GetSystemSetLanguage("OtherCustomerPicPage", "lbSummary", language, path);
            this.AddBtn.Text = ManageSystemConfig.GetSystemSetLanguage("OtherCustomerPicPage", "AddBtn", language, path);
        }

        private void SetFromImage()
        {
            switch (this.Session["language"].ToString())
            {
                case "cn":
                    this.iBtnCustomer.ImageUrl = "../images/title_customerinfo.jpg";
                    this.iBtnOrange0.ImageUrl = "../images/title_CustomerInfo_lable_1_orange.jpg";
                    this.iBtnOrange1.ImageUrl = "../images/title_CustomerInfo_lable_2_orange.jpg";
                    this.iBtnOrange2.ImageUrl = "../images/title_CustomerInfo_lable_3_orange.jpg";
                    this.iBtnOrange3.ImageUrl = "../images/title_CustomerInfo_lable_4_orange.jpg";
                    this.iBtnOrange4.ImageUrl = "../images/title_CustomerInfo_lable_5_orange.jpg";
                    this.iBtnOrange5.ImageUrl = "../images/title_CustomerInfo_lable_6_orange.jpg";
                    this.iBtnOrange6.ImageUrl = "../images/title_CustomerInfo_lable_7_orange.jpg";
                    this.iBtnOrange7.ImageUrl = "../images/title_CustomerInfo_lable_8_orange.jpg";
                    this.iBtnGray8.ImageUrl = "../images/title_CustomerInfo_lable_9_Gray.jpg";
                    break;

                case "en":
                    this.iBtnCustomer.ImageUrl = "../images/title_customerinfoEn.jpg";
                    this.iBtnOrange0.ImageUrl = "../images/title_CustomerInfoEn_lable_1_orange.jpg";
                    this.iBtnOrange1.ImageUrl = "../images/title_CustomerInfoEn_lable_2_orange.jpg";
                    this.iBtnOrange2.ImageUrl = "../images/title_CustomerInfoEn_lable_3_orange.jpg";
                    this.iBtnOrange3.ImageUrl = "../images/title_CustomerInfoEn_lable_4_orange.jpg";
                    this.iBtnOrange4.ImageUrl = "../images/title_CustomerInfoEn_lable_5_orange.jpg";
                    this.iBtnOrange5.ImageUrl = "../images/title_CustomerInfoEn_lable_6_orange.jpg";
                    this.iBtnOrange6.ImageUrl = "../images/title_CustomerInfoEn_lable_7_orange.jpg";
                    this.iBtnOrange7.ImageUrl = "../images/title_CustomerInfoEn_lable_8_orange.jpg";
                    this.iBtnGray8.ImageUrl = "../images/title_CustomerInfoEn_lable_9_Gray.jpg";
                    break;
            }
        }

        protected string ValidateDateTime(object tmpObj)
        {
            string strTmp = tmpObj.ToString();
            if (strTmp.Substring(0, 4) == "1800")
            {
                return "---- -- --";
            }
            return strTmp;
        }
    }
}

